package com.auemall.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auemall.constant.ApplicationConstant;
import com.auemall.service.ItemCatService;
import com.auemall.utils.EUTreeNode;
import com.auemall.vo.BaseRespVO;
import com.auemall.vo.BindCatBrandReqVO;
import com.auemall.vo.CatListVO;
import com.auemall.vo.SelectAllCatListRespVO;
import com.auemall.vo.SelectBindBrandRespVO;


/**
 * 商品分类管理controller
 * <p>Title: ItemCatController</p>
 * <p>Description: </p>
 *
 */
@Controller
@RequestMapping("/item/v1/cat")
public class ItemCatController {
    
    private Logger logger = Logger.getLogger(ItemCatController.class);

	@Autowired
	private ItemCatService itemCatService;
	
	@RequestMapping("/list")
	@ResponseBody
	private List<EUTreeNode> selectCatList(@RequestParam(value="id",defaultValue="0")int parentId) {
	    List<EUTreeNode> list = itemCatService.getCatList(parentId);
	    return list;
	}
	
	@RequestMapping("/alllist")
    @ResponseBody
    private SelectAllCatListRespVO selectAllCatList() {
	    SelectAllCatListRespVO respVO;
        try{
            respVO = itemCatService.selectAllCatList();
        }catch(Exception e){
            respVO = new SelectAllCatListRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("查询所有商品分类菜单异常（/item/cat/alllist）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/insertcat")
    @ResponseBody
    private BaseRespVO insertCat(@RequestBody CatListVO req) {
	    BaseRespVO respVO;
        try{
            respVO = itemCatService.insertCat(req);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("插入商品分类菜单异常（/item/cat/insertcat）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/updatecat")
    @ResponseBody
    private BaseRespVO updateCat(@RequestBody CatListVO req) {
        BaseRespVO respVO;
        try{
            respVO = itemCatService.updateCat(req);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("更新商品分类菜单异常（/item/cat/updatecat）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/deletecat")
    @ResponseBody
    private BaseRespVO deleteCat(@RequestBody String id) {
        BaseRespVO respVO;
        try{
            respVO = itemCatService.deleteCat(id);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("删除商品分类菜单异常（/item/cat/deletecat）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/bindcatbrand")
    @ResponseBody
    private BaseRespVO bindCatBrand(@RequestBody BindCatBrandReqVO req) {
        BaseRespVO respVO;
        try{
            respVO = itemCatService.bindCatBrand(req);
        }catch(Exception e){
            respVO = new BaseRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("商品分类与品牌绑定异常（/item/cat/bindcatbrand）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
	
	@RequestMapping("/selectbindbrand")
    @ResponseBody
    private SelectBindBrandRespVO selectBindBrand(@RequestBody String categoryId) {
	    SelectBindBrandRespVO respVO;
        try{
            respVO = itemCatService.selectBindBrand(categoryId);
        }catch(Exception e){
            respVO = new SelectBindBrandRespVO();
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("查询与传入类别关联的品牌异常（/item/cat/selectbindbrand）："+e.getMessage());
            logger.error(respVO.getMsg());
        }
        return respVO;
    }
}
